package pl.wr.particle.impl.quantum;

import pl.wr.particle.def.quantum.IElectronCloud;
import pl.wr.particle.def.quantum.IElectronShell;
import pl.wr.physics.exceptions.QuantumException;

/**
 * @version 1.0
 * @author wieslaw.rodak
 * 
 */
public class ElectronCloud implements IElectronCloud {

    private IElectronShell[] electronShells;

    /**
     * @param electrons
     *            - The count of electrons
     * @throws QuantumException
     */
    public ElectronCloud(int electrons) {
        fillElectronShells(electrons);
    }

    @Override
    public IElectronShell[] getElectronShells() {
        return electronShells;
    }

    /**
     * @param shells
     *            - count of new shells
     * @throws QuantumException
     */
    private void makeElectronShells(int shells, int electrons) {

        electronShells = new IElectronShell[shells];

        switch (shells) {

        case 7:
            electronShells[6] = new ElectronShell(Shell.Q, electrons - 182); // -2-8-18-32-50-72
        case 6:
            electronShells[5] = new ElectronShell(Shell.P, electrons - 110); // -2-8-18-32-50
        case 5:
            electronShells[4] = new ElectronShell(Shell.O, electrons - 60); // -2-8-18-32
        case 4:
            electronShells[3] = new ElectronShell(Shell.N, electrons - 28); // -2-8-18
        case 3:
            electronShells[2] = new ElectronShell(Shell.M, electrons - 10); // -2-8
        case 2:
            electronShells[1] = new ElectronShell(Shell.L, electrons - 2);
        case 1:
            electronShells[0] = new ElectronShell(Shell.K, electrons);
            break;

        default:
            throw new QuantumException("Nieprawidłowa powłoka");
        }

    }

    private void fillElectronShells(int electrons) {

        int shells = 0;

        if (electrons <= 0) {
            throw new QuantumException("Nieprawidłowa liczba elektronów");
        } else if (electrons <= 2) {
            shells = 1;
        } else if (electrons <= 10) { // 2+8
            shells = 2;
        } else if (electrons <= 28) { // 10+18
            shells = 3;
        } else if (electrons <= 60) { // 28+32
            shells = 4;
        } else if (electrons <= 110) { // 60+50
            shells = 5;
        } else if (electrons <= 182) { // 110+72
            shells = 6;
        } else if (electrons <= 280) { // 182+98
            shells = 7;
        } else {
            throw new QuantumException("Nieprawidłowa liczba elektronów");
        }

        makeElectronShells(shells, electrons);

    }

}
